//
//  RuleViewController2.m
//  YunGui
//
//  Created by Hanen 3G 01 on 16/5/4.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "RuleViewController2.h"
#import "PopTipView.h"
#import "QBImagePickerController.h"
#import "ShowPhotoViewController.h"
#import "CameraViewController.h"
#import "NetModel.h"
@interface RuleViewController2 ()
{
    UILabel *numberlabel;
    UILabel *nameLabel;
    UILabel *ruleLabel;
    CGFloat   space;
    
    CGFloat lastY;
    CGFloat lastX;
    UIScrollView *_scrollView;
    
    UIButton *btn;
 

    
    
    NSDictionary  *_dataDict;
}
@end

@implementation RuleViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = GrayBackColor;
//    self.navigationItem.leftBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithAction:@selector(backController)];
    //    self.navigationController.navigationBar.backgroundColor = GrayBackColor;
    //    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.title = @"检查标准";
    
    space = [UIView getWidth:10.0f];

    [self addsubViews];
    
//    if (!self.inCameraChoose) {
//        [self addBotom];
//    }else{
        self.navigationController.navigationBarHidden = YES;
        
        UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 ,kScreenWidth, 64)];
        navView.backgroundColor = BlueColor;
        
        [self.view addSubview:navView];
        
        UIButton *btnb = [[UIButton alloc] initWithFrame:CGRectMake( 24 , 20+(44 - [UIView getWidth:20])/2, [UIView getWidth:20.0f], [UIView getWidth:20.0f])];
        //        btnb .backgroundColor = BlueColor;
        [btnb setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
        [btnb addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
        [navView addSubview:btnb];
    
    UILabel *title = [ViewTool getLabelWith:CGRectMake(kScreenWidth / 2.0f - 50 , 24+1, 100, 30.0f) WithTitle:@"检查标准" WithFontSize:18.0f WithTitleColor:[UIColor whiteColor] WithTextAlignment:NSTextAlignmentCenter];
    [navView addSubview:title];
//    }
    
    [self loadCheckRuleData];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    //    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
}
- (void)loadCheckRuleData
{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"method":@"app.branchCheck.check.standard",@"check_id":self.check_id,@"type": self.ruleType}];
    
    NSDictionary *paramersDict = [DataTool addSignToDic:dict];
    [DataTool postWithUrl:HEAD_URL parameters:paramersDict success:^(id data) {
        
        id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
        NSLog(@"评定标准 数据%@",backData);
        _dataDict = backData[@"data"];
        btn.enabled = YES;
        [self useData];
    } fail:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
- (void)useData
{
        numberlabel.text = _dataDict[@"branch_no"];
        nameLabel.text = _dataDict[@"branch_name"];
    
    NSString *str;
    if ([self.ruleType intValue] == 0) {
        str =  [NSString stringWithFormat:@"雨棚评价标准(最少拍摄%d张照片)",[_dataDict[@"page_min"] intValue]];
    }else if ([self.ruleType intValue] == 1){
        str = [NSString stringWithFormat:@"地坪评价标准(最少拍摄%d张照片)",[_dataDict[@"page_min"] intValue]];
    }else{
        str = [NSString stringWithFormat:@"柜体评价标准(最少拍摄%d张照片)",[_dataDict[@"page_min"] intValue]];//照片 可以有前面的数据 根据类型 获取pageMin值
    }
    ruleLabel.text = str;
    
    
}
//- (void)addBotom
//{
//    UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - [UIView getHeight:66.0f] - 64.0f, kScreenWidth, [UIView getHeight:70.0f])];
//    iv.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:iv];
//    
//    btn = [[UIButton alloc] initWithFrame:CGRectMake([UIView getWidth:15], [UIView getHeight:13.0f], kScreenWidth - 2 * [UIView getWidth:15], iv.height - 2 * [UIView getHeight:13.0f])];
//    btn.center = CGPointMake(self.view.width / 2.0f, iv.height / 2.0f);
//    [btn setTitle:@"开始拍摄" forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    btn.titleLabel.font =[UIFont systemFontOfSize:18.0f];
//    btn.backgroundColor = BlueColor;
//    [iv addSubview:btn];
//    btn.enabled = NO;
//    [btn addTarget:self action:@selector(clickCheck22) forControlEvents:UIControlEventTouchUpInside];
//    iv.userInteractionEnabled = YES;
//    
//    
//    
//}
- (void)addsubViews
{
    
    //    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 ,kScreenWidth, 64)];
    //    navView.backgroundColor = GrayBackColor;
    //
    //    [self.view addSubview:navView];
    //
    //    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake( space , space + 24.0f, 20.0f, 20.0f)];
    //    btn .backgroundColor = BlueColor;
    //   [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    //    [navView addSubview:btn];
    
    CGFloat y = 64.0f;
  
   
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, y, kScreenWidth, kScreenHeight  - y)];
    _scrollView.backgroundColor  = [UIColor whiteColor];
    [self.view addSubview:_scrollView];
    
    UILabel *label1 = [ViewTool getLabelWith:CGRectMake(30.0f,  space , 70.0f, 20.0f) WithTitle:@"网点编号" WithFontSize:15.0f WithTitleColor:SUBTITLECOLOR WithTextAlignment:NSTextAlignmentLeft];
    [_scrollView addSubview:label1];
    
    NSLog(@"%@",self.branch_no);
    numberlabel = [ViewTool getLabelWith:CGRectMake(label1.maxX + space,  label1.y , kScreenWidth - label1.maxX - space, 20.0f) WithTitle:self.branch_no WithFontSize:16.0f WithTitleColor:BLACKCOLOR WithTextAlignment:NSTextAlignmentLeft];
    //    numberlabel.backgroundColor = BlueColor;
    [_scrollView addSubview:numberlabel];
    
    UILabel *label2 = [ViewTool getLabelWith:CGRectMake(label1.x,  label1.maxY + space , 70.0f, 20.0f) WithTitle:@"网点名称" WithFontSize:15.0f WithTitleColor:SUBTITLECOLOR WithTextAlignment:NSTextAlignmentLeft];
    [_scrollView addSubview:label2];
    
    nameLabel = [ViewTool getLabelWith:CGRectMake(label2.maxX + space, label2.y, kScreenWidth - label2.maxX - space, 20.0f) WithTitle:self.branach_name WithFontSize:16.0f WithTitleColor:BLACKCOLOR WithTextAlignment:NSTextAlignmentLeft];
    [_scrollView addSubview:nameLabel];
    
    
    UIView *lien = [ViewTool getLineViewWith:CGRectMake(label2.x, nameLabel.maxY + space, kScreenWidth - label2.x, 1) withBackgroudColor:GRAYLINECOLOR];
    [_scrollView addSubview:lien];
    
    
    ruleLabel = [ViewTool getLabelWith:CGRectMake(label2.x, lien.maxY + space , kScreenWidth - label2.x, 20.0f) WithTitle:@"  " WithFontSize:18.0f WithTitleColor:BLACKCOLOR WithTextAlignment:NSTextAlignmentLeft];
    [_scrollView addSubview:ruleLabel];
    
    CGFloat sectionH = [UIView getHeight:280.0f];
    CGFloat iamgeW = self.view.width - 10 * space;
    CGFloat imageH = [UIView getHeight:200.0f];
    
    NSArray * titleArra1 = @[@"一、2张地坪厚度：地坪左右各一张厚度照片。",@"二、2张地坪宽度照片：地坪左右各一张宽度照片。"];
    NSArray * titleArra2 = @[@"一 1张所有柜体打开1/6/10/11/16/20号柜门的全景照片",@"二、多张柜体正、反面三代柜中间加强板组焊件照片。",@"三、1张柜体左加固件。图片要求至少能够显示左侧三个加固件。",@"四、1张柜体右加固件。图片要求至少能够显示右侧三个加固件。",@"五、1张柜内左加强构件。图片能够看清左边柜内L形加固件"];
    NSArray * titleArra3 = @[@"(1).雨棚和雨棚M8连接螺栓是否安装牢固。",@"(2).检查雨棚侧边是否安装堵头。"];
    
    NSArray * imageArra1 = @[@"dp_1.jpg",@"dp_3.jpg"];//地坪
    NSArray * imageArra2 = @[@"guiti_1_1.jpg",@"guiti_2.png",@"guiti_3.jpg",@"guiti_5.jpg",@"guiti_6.jpg"];
    NSArray * imageArra3 = @[@"yupeng_1.jpg",@"yupeng_3.jpg"];
    
    NSArray * arr = [NSArray array];
    NSArray *imageArr = [NSArray array];
    
    if([_ruleType intValue] == 1){
        arr = titleArra1;
        imageArr = imageArra1;
    }else if ([_ruleType intValue] == 2){
        arr = titleArra2;
        imageArr = imageArra2;
    }else{
        arr = titleArra3;
        imageArr = imageArra3;
    }
    CGFloat labelH = 40.0f;
    CGFloat labelW = kScreenWidth - lien.x;
    
    //先排布 前面四个
    for (int i = 0; i<arr.count ; i++) {
        UILabel *label = [ViewTool getLabelWith:CGRectMake(lien.x, ruleLabel.maxY + space +(sectionH) * i, labelW, labelH) WithTitle:arr[i] WithFontSize:14.0f WithTitleColor:BLACKCOLOR WithTextAlignment:NSTextAlignmentLeft];
        label.numberOfLines = 0;
        //        label.backgroundColor = BlueColor;
        [_scrollView addSubview:label];
        
        UIImageView *iamgeView = [[UIImageView alloc] initWithFrame:CGRectMake( 5 * space, label.maxY , iamgeW, imageH)];
        iamgeView.backgroundColor = GrayBackColor;
        //        UIImage *image = [UIImage imageWithContentsOfFile:imageArr[i]];
        iamgeView.image = [UIImage imageNamed:imageArr[i]];
        [_scrollView addSubview:iamgeView];
        
        if (i == arr.count - 1) {//到时候 设定数组最后一个
            lastX = iamgeView.maxY;
        }
        
    }
    
    
    
    //    CGFloat imageViewW = 50.0f;
    //
    //
    //    for (int i = 0; i< 7; i++) {
    //        CGFloat row = i / 3;
    //        CGFloat col = i % 3;
    //        UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(label.maxX +space + (imageViewW + space) *col, label.y + (space + imageViewW) * row, imageViewW, imageViewW)];
    //        imageView.backgroundColor = [UIColor redColor];
    //        [_scrollView addSubview:imageView];
    //    }
   
        _scrollView.contentSize = CGSizeMake(kScreenWidth,lastX +space + [UIView getHeight:70.0f]);
    
    //    UIView *imageView = [[UIView alloc] initWithFrame:CGRectMake(label.maxX +space, label.y, kScreenWidth - (label.maxX +space), 100)];
    //    imageView.backgroundColor = [UIColor redColor];
    //    [self.view addSubview:imageView];
    
    
}
#pragma mark --开始拍摄
//- (void)clickCheck22
//{
//    _choosePicDataArray = [NSMutableArray array];
//    _imagarr2 = [NSMutableArray array];
//#warning 获取网上的数据 要有的话 直接进入展示图片页面
//    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{@"method":@"app.branchCheck.photo",@"detail_id": _dataDict[@"detail_id"]}];//需要网点检查ID
//    //            [talkingArray removeAllObjects];
//    NSDictionary *paramersDict = [DataTool addSignToDic:dict];
//    [DataTool postWithUrl:HEAD_URL parameters:paramersDict success:^(id data) {
//        id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
//        //        NSDictionary *dict = backData[@"baseInfo"];
//        NSLog(@"图片详情的%@",backData);
//        
//        [SVProgressHUD dismiss];
//        //            CabinetCheckDetailController *cabeVC = [[CabinetCheckDetailController alloc] init];
//        //            [self.navigationController popToViewController:cabeVC animated:YES];
//        NSArray *iamgeArr = backData[@"data"][@"imgData"];
//        if ([iamgeArr count] != 0) {
//            ShowPhotoViewController *show = [[ShowPhotoViewController alloc] init];
//            for (int i = 0; i< [iamgeArr count] - 1; i++) {
//                ImageModel *model = [[ImageModel alloc] init];
//                model.imageId = iamgeArr[i][@"id"];
//                model.url = iamgeArr[i][@"url"];
//                model.file_name = iamgeArr[i][@"file_name"];
//                model.file_type = iamgeArr[i][@"file_type"];
//                model.created_at = iamgeArr[i][@"created_at"];
//                model.detail_id = iamgeArr[i][@"detail_id"];
//                [_choosePicDataArray addObject:model];
//                [_imagarr2 addObject:model];
//            }
//            
//            //            model.imageId = [backData[@"data"][@"imgData"]
//            show.imageArr = _choosePicDataArray;
//            show.iamgeSmallArr = _imagarr2;
//            show.chooseType  = 2;
//            //            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:show];
//            show.checkType = self.ruleType;
//            show.branchId = _dataDict[@"branch_id"];
//            //            [self presentViewController:nav animated:NO completion:nil];
//            show.upLoadNumber = [_dataDict[@"page_min"] intValue];
//            show.checkID = _dataDict[@"check_id"];
//#warning 更换切换方式
//            [self.navigationController pushViewController:show animated:NO];
//            
//        }else{
//            [self popvIew];
//        }
//    } fail:^(NSError *error) {
//        NSLog(@"%@",error.localizedDescription);
//    }];
//    
//    
//}

- (void)clickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark --点击确定或删除
//- (void)clickLeftbtnWith:(PopTipView *)popView{
//    [popChooseCamera dimiss];
//    CameraViewController *cameraVC = [[CameraViewController alloc] init];
//    //        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:cameraVC];
//    cameraVC.checkType = self.ruleType ; // 0 ,1 ,2
//    //    [self presentViewController:cameraVC animated:YES completion:nil];
//    cameraVC.checkId = _dataDict[@"check_id"];
//    cameraVC.branchId = _dataDict[@"branch_id"];
//    cameraVC.chooset = 2;
//    cameraVC.uploadNum = [_dataDict[@"page_min"] intValue];
//    cameraVC.isPush = YES;
//    
//    [self.navigationController pushViewController:cameraVC animated:NO];
//}
//- (void)clickRightbtnWith:(PopTipView *)popView{
//    [popChooseCamera dimiss];
//    [self takePictureInPhone1];
//    
//}
//- (void)takePictureInPhone1
//{
//    if (![QBImagePickerController isAccessible]) {
//        [Tools showMessage:@"相机不可用哦" view:self];
//    }
//    QBImagePickerController *imagePickerController = [[QBImagePickerController alloc] init];
//    imagePickerController.delegate = self;
//    imagePickerController.allowsMultipleSelection = YES;
//    imagePickerController.maximumNumberOfSelection = 6;//最大上传5张
//    
//    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:imagePickerController];
//    [self presentViewController:navigationController animated:YES completion:NULL];
//}
#pragma mark -- QBImage代理
//- (void)imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets
//{
//    
//    for(int i = 0 ;i< assets.count; i ++){
//        ALAssetRepresentation *resentation = [assets[i]  defaultRepresentation];
//        UIImage *image = [UIImage imageWithCGImage:[resentation fullResolutionImage]];
//        
//        NSData *data = UIImageJPEGRepresentation(image, 0.35);
//        
//        NSData *data1 = UIImageJPEGRepresentation(image, 0.2);
//        UIImage *image2 = [UIImage imageWithData:data1];
//        data1 = [self imageWithImage:image2 scaledToSize:CGSizeMake((kScreenWidth - 5.0f * 3) / 4.0f, (kScreenWidth - 5 * 3) / 4.0f)];
//        [_imagarr2 addObject:data1];
//        
//        [_choosePicDataArray addObject:data];
//        
//    }
//    if (self.presentedViewController) {
//        [self dismissViewControllerAnimated:YES completion:^{
//            ShowPhotoViewController *show = [[ShowPhotoViewController alloc] init];
//            show.imageArr = _choosePicDataArray;
//            //            show.iamgeSmallArr = nil;
//            show.iamgeSmallArr = _imagarr2;
//            show.chooseType  = 2;
//            //            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:show];
//            show.checkType = self.ruleType;
//            
//            show.branchId = _dataDict[@"branch_id"];
//            show.checkID = self.check_id;
//            show.upLoadNumber = [_dataDict[@"page_min"] intValue];
//            show.checkID = _dataDict[@"check_id"];
//            //            [self presentViewController:nav animated:NO completion:nil];
//#warning 更换切换方式
//            [self.navigationController pushViewController:show animated:NO];
//        }];
//    }
//    //    NSLog(@"选择照片的数组个数%d",_choosePicDataArray.count);
//    //    [self dismissImagePickerController];
//    
//    //    NSLog(@"选择照片的数组个数 ===%ld",_choosePicDataArray.count);
//    
//    
//    //    picVew.images = _choosePicDataArray;
//    
//    
//}
//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
//{
//    [picker dismissViewControllerAnimated:YES completion:nil];
//}
//- (void)dismissImagePickerController
//{
//    if (self.presentedViewController) {
//        [self dismissViewControllerAnimated:YES completion:NULL];
//    }
//}

//- (void)imagePickerControllerDidCancel1:(QBImagePickerController *)imagePickerController
//{
//    //    NSLog(@"*** imagePickerControllerDidCancel:");
//    
//    [self dismissImagePickerController];
//}

//- (NSData *)imageWithImage:(UIImage*)image
//              scaledToSize:(CGSize)newSize;
//{
//    UIGraphicsBeginImageContext(newSize);
//    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
//    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    return UIImageJPEGRepresentation(newImage, 0.8);
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载，在WWDC视频也忽视这一点。
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
    }
    
    dispatch_async(
                   dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
                   , ^{
                       NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
                       
                       NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];
//                       NSLog(@"files :%d",[files count]);
                       for (NSString *p in files) {
                           NSError *error;
                           NSString *path = [cachPath stringByAppendingPathComponent:p];
                           if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
                               [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
                           }
                       };});
    [[SDImageCache sharedImageCache] clearDisk];//清理磁盘
    
}
- (void)backController
{
//    if(self.inCameraChoose){
//        [self.navigationController popViewControllerAnimated:YES];
//    }else{
//        [self.navigationController popViewControllerAnimated:YES];
//        
//    }
    [self dismissViewControllerAnimated:NO completion:nil];
    //    if (self.ispush) {
    //        for (UIViewController *vo in self.navigationController.viewControllers) {
    //            if ([vo isKindOfClass:[ShowPhotoViewController class]]) {
    //                ShowPhotoViewController *show = (ShowPhotoViewController *)vo;
    //                [self.navigationController popToViewController:show animated:NO];
    //            }
    //        }
    //    }
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */



@end
